//
//  HHSinglePickerView.m
//  Flower
//
//  Created by 花花 on 2016/12/13.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHSinglePickerView.h"
#import "HHToolBar.h"
//extern void ifDebug(dispatch_block_t blcok);
@interface HHSinglePickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic, strong) UIPickerView *pickerView;

@property(nonatomic, strong) HHToolBar *toolBar;

@property(nonatomic, strong) NSArray<NSString *> *data;

@property(nonatomic, strong) NSString *selectedValue;

@property(nonatomic,assign)NSInteger selectedIndex;

@property(nonatomic, copy) SingleSelectedHandler selectedValueChangeHandler;


@end
@implementation HHSinglePickerView





-(instancetype)initWithToolBarText:(NSString *)toolBarText withDefaultIndex:(NSInteger)defalutIndex withData:(NSArray<NSString*>*)data withValueDidChangeHandler:(SingleSelectedHandler)valueDidChangeHandler cancelOnClick:(BtnOnClick)cancelOnClick doneOnClick:(SingleDoneHandler)doneOnClick{

    
    if (self = [super init]) {
        
        if (!data || ![data isKindOfClass:[NSArray class]]) {
//            ifDebug(^{
//                NSAssert(NO, @"设置的数据格式不正确,初始化失败");
//            });
//            
            return nil;
        }
        _data = data;
        
        _selectedValueChangeHandler = [valueDidChangeHandler copy];
        if (defalutIndex <0 || defalutIndex>=data.count) {
//            ifDebug(^{
//               NSLog(@"设置的默认下标不合法, 将被设置为0");
//            });
            defalutIndex = 0;
            
            
        }
        _selectedIndex = defalutIndex;
        
        //验证完毕，选择值改变
        [self setupSelectedValueDidChange];
        
        __weak typeof(self)weakSelf = self;
        _toolBar = [[HHToolBar alloc]initWithToolbarText:toolBarText cancelOnClick:cancelOnClick doneOnClick:^{
            __strong typeof(self) strongSelf = weakSelf;
            if (strongSelf) {
                
                if (doneOnClick) {
                    doneOnClick(strongSelf.selectedIndex,strongSelf.selectedValue);
                }
            }
            
            
        }];
        
        [self.pickerView selectRow:_selectedIndex inComponent:0 animated:NO];
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.pickerView];
        [self addSubview:_toolBar];
        
    }

    return self;


}


-(void)layoutSubviews{

    [super layoutSubviews];
    //公式:Item.attr1  = Item2.attr2 * nultiplier + constant
    //self.toolBard的左 = self的左*1 + 0；
    
    CGFloat kToolBarHeight = 44;
    NSLayoutConstraint *toolBarLeft = [NSLayoutConstraint constraintWithItem:self.toolBar attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    
    NSLayoutConstraint *tooBarRight =[NSLayoutConstraint constraintWithItem:self.toolBar attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    
    NSLayoutConstraint *toolBarHeight =[NSLayoutConstraint constraintWithItem:self.toolBar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:kToolBarHeight];
    
    NSLayoutConstraint *toolBarTop =[NSLayoutConstraint constraintWithItem:self.toolBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    self.toolBar.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraints:@[toolBarLeft,tooBarRight,toolBarHeight,toolBarTop]];
    
    
    NSLayoutConstraint *pickerViewLeft = [NSLayoutConstraint constraintWithItem:self.pickerView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    
    NSLayoutConstraint *pickerViewRight =[NSLayoutConstraint constraintWithItem:self.pickerView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    
    NSLayoutConstraint *pickerViewHeight =[NSLayoutConstraint constraintWithItem:self.pickerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:self.bounds.size.height -kToolBarHeight];
    
    NSLayoutConstraint *pickerViewBottom =[NSLayoutConstraint constraintWithItem:self.pickerView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    self.pickerView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraints:@[pickerViewLeft,pickerViewRight,pickerViewHeight,pickerViewBottom]];
    
    
    
    


}

-(void)setupSelectedValueDidChange{

    if (_selectedValueChangeHandler) {
        _selectedValueChangeHandler(self.selectedValue);
    }


}


#pragma mark - <<UIPickerViewDelegate,UIPickerViewDataSource>>
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 1;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    return self.data.count;

}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    _selectedIndex  = row;
    
    [self setupSelectedValueDidChange];


}
-(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = NSTextAlignmentCenter;
    label.font =[UIFont systemFontOfSize:18];
    label.backgroundColor =[UIColor clearColor];
    label.adjustsFontSizeToFitWidth = YES;
    label.text = self.data[row];
    label.textColor =[UIColor blackColor];
    return label;
    

}
#pragma mark - Setter && Getter
-(NSString *)selectedValue{
    
    if (_selectedIndex>= 0&& _selectedIndex<self.data.count) {
        _selectedValue = self.data[_selectedIndex];
    }
    return _selectedValue;
    

}

-(NSArray<NSString *> *)data{

    if (!_data) {
        _data =[NSArray array];
    }

    return _data;
}

-(UIPickerView *)pickerView{

    if (!_pickerView) {
        _pickerView =[[UIPickerView alloc]init];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    return _pickerView;


}


@end
